package repository

import (
	"context"

	"server/internal/app/model"
	"server/internal/core/store"

	"gorm.io/gorm"
)

type SysRoleRepository struct {
	db *gorm.DB
}

func NewSysRoleRepository() *SysRoleRepository {
	return &SysRoleRepository{db: store.DB}
}

func (r *SysRoleRepository) All(ctx context.Context) ([]model.SysRole, error) {
	var roles []model.SysRole
	err := r.db.WithContext(ctx).Find(&roles).Error
	return roles, err
}

func (r *SysRoleRepository) Create(ctx context.Context, role *model.SysRole) error {
	return r.db.WithContext(ctx).Create(role).Error
}

func (r *SysRoleRepository) Update(ctx context.Context, id uint, data map[string]any) error {
	return r.db.WithContext(ctx).Model(&model.SysRole{}).Where("id = ?", id).Updates(data).Error
}

func (r *SysRoleRepository) Delete(ctx context.Context, id uint) error {
	return r.db.WithContext(ctx).Delete(&model.SysRole{}, id).Error
}
